//
//  HJAppInfo.m
//  KaiChat
//
//  Created by lx on 2023/10/17.
//

#import "HJAppInfo.h"
#import <UIKit/UIKit.h>
#import "HJKeychain.h"
#import <sys/utsname.h>


static NSString *const kDevBundleIdKey = @"com.xtjun.KaiChat";
static NSString *const kProBundleIdKey = @"com.kgcl.kai";
static NSString *const kGoogleClientIDKey = @"clientID";
static NSString *const kGoogleRecaptchaSiteKey = @"recaptchaSiteKey";
static NSString *const kBaseDomainsKey = @"baseDomains";
static NSString *const kWebSoctetUrlKey = @"webSoctetUrl";
static NSString *const kKaiPayUrlKey = @"KaiPayUrl";
static NSString *const kShareUrlKey = @"shareUrlKey";
static NSString *const kOneSignalAppIdKey = @"oneSignalAppIdKey";

static NSDictionary *appConfiguration = nil;

extern NSDictionary *getAppConfiguration(void) {
    if (!appConfiguration) {
        appConfiguration = @{
            kBaseDomainsKey: @{
                @(HJAppEnvTypeDev): @"test-api-ai.ai-chat.ltd",
                @(HJAppEnvTypePre): @"stg-app-api.ai-chat.ltd",
                @(HJAppEnvTypePro): @"api.kaichats.com",
            },
            kWebSoctetUrlKey: @{
                @(HJAppEnvTypeDev): @"ws://test-api-ai.ai-chat.ltd/sub",
                @(HJAppEnvTypePre): @"ws://stg-app-api.ai-chat.ltd/sub",
                @(HJAppEnvTypePro): @"wss://api.kaichats.com/sub",
            },
            kKaiPayUrlKey: @{
                @(HJAppEnvTypeDev): @"http://test-pay.kai.com",
                @(HJAppEnvTypePre): @"http://pay.kaichat.io",
                @(HJAppEnvTypePro): @"http://pay.kaichat.io",
            },
            kShareUrlKey: @{
                @(HJAppEnvTypeDev): @"http://test-event.kaiaction.com/pages",
                @(HJAppEnvTypePre): @"http://eks-activity.ai-chat.ltd/pages",
                @(HJAppEnvTypePro): @"https://event.kaiaction.com/pages",
            },
            kDevBundleIdKey: @{
                kGoogleRecaptchaSiteKey : @"6Lc6llgpAAAAAE1hgl-O2pzVOKP7_c3Q8XRcKDsG",
                kGoogleClientIDKey : @"90517965910-j5u89g6g1v0uk675t0djl4n3aouful1l.apps.googleusercontent.com",
                kOneSignalAppIdKey: @"508a0b02-830d-445c-be8d-8f422d2d9cd4"
            },
            kProBundleIdKey: @{
                kGoogleRecaptchaSiteKey : @"6Lexa2EpAAAAAFumRxRHSzNgF9ZCkiAorwbNleMt",
                kGoogleClientIDKey : @"90517965910-r1sd9u5i57q5nu4un0gsj70n85l4sru5.apps.googleusercontent.com",
                kOneSignalAppIdKey: @"73b80d55-32f6-42ac-8d45-c3446c9f5e68"
            },
        };
    }
    return appConfiguration;
}

HJAppEnvType getAppEnvType(void) {
    return HJAppEnvTypePro;
    if ([getAppBundleIdentifier() isEqualToString:kProBundleIdKey]) return HJAppEnvTypePro;
    return HJAppEnvTypePro;
}

BOOL isDevelopment(void) {
    return getAppEnvType() == HJAppEnvTypeDev;
}

BOOL isProduction(void) {
    return getAppEnvType() == HJAppEnvTypePro;
}

NSString *getAppId(void) {
    return @"6474106193";
}

NSString *getAppsFlyerKey(void) {
    return @"jnHgnVEiVHeoiE4SbfsW8k";
}

NSString *getBaseDomains(void) {
    return getAppConfiguration()[kBaseDomainsKey][@(getAppEnvType())];
}

NSString *getWebSoctetUrl(void) {
    return getAppConfiguration()[kWebSoctetUrlKey][@(getAppEnvType())];
}

NSString *getKaiPayUrl(void) {
    return getAppConfiguration()[kKaiPayUrlKey][@(getAppEnvType())];
}

NSString *getBaseUrlString(void) {
    if (getAppEnvType() == HJAppEnvTypePro) {
        return [NSString stringWithFormat:@"https://%@", getBaseDomains()];
    }
    return [NSString stringWithFormat:@"http://%@", getBaseDomains()];
}

NSString *getRecaptchaKey(void) {
    return getAppConfiguration()[getAppBundleIdentifier()][kGoogleRecaptchaSiteKey];
}

NSString *getGoogleGIDClientID(void) {
    return getAppConfiguration()[getAppBundleIdentifier()][kGoogleClientIDKey];
}

NSString *getGroupShareUrl(void) {
    return [NSString stringWithFormat:@"%@/join-group", getAppConfiguration()[kShareUrlKey][@(getAppEnvType())]];
}

NSString *getNewsShareUrl(void) {
    return [NSString stringWithFormat:@"%@/article", getAppConfiguration()[kShareUrlKey][@(getAppEnvType())]];
}

NSString *getOneSignalAppId(void) {
    return getAppConfiguration()[getAppBundleIdentifier()][kOneSignalAppIdKey];
}

BOOL isIPhoneX(void) {
    if (@available(iOS 11.0, *)) {
        CGFloat height = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom;
        return (height > 0);
    } else {
        return NO;
    }
}

NSString *getDeviceSystemVersion(void) {
    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    return systemVersion;
}

NSString *getAppVersion(void) {
    NSDictionary *infoDictionary = [NSBundle.mainBundle infoDictionary];
    return infoDictionary[@"CFBundleShortVersionString"];
}

NSString *getAppName(void) {
    NSDictionary *infoDictionary = [NSBundle.mainBundle infoDictionary];
    return infoDictionary[@"CFBundleDisplayName"];
}

NSString *getAppBundleIdentifier(void) {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
}

NSString *getUserAgent(void) {
    return [NSString stringWithFormat:@"Mozilla/5.0 %@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
}

NSString *getLocaleIdentifier(void) {
    return @"en_US";
}

//dd MMM yyyy HH:mm
//yyyy-MM-dd HH:mm

NSString *getYYMMDDDate(void) {
    return @"en_US";
}

NSString *getMMDDDate(void) {
    return @"MMMM d";
}

BOOL isUseCrypto(void) {
    return NO;
}

BOOL isHiddenAppleServe(void) {
    return NO;
}

BOOL isTestFight(void) {
    return NO;
}

NSString *getAppChannel(void) {
    if (isTestFight()) {
        return @"testfight";
    }
    return @"ios";
}

NSString *getDeviceUDID(void) {
    NSString *object = HJKeychain.UDID;
    if (object.length) {
        return object;
    }
    object = getMessageId();
    HJKeychain.UDID = object;
    return object;
}

NSString *getBuglyAppId(void) {
    return @"61f72c93bb";
}

static NSDictionary *deviceDictionary = nil;
NSDictionary * getDeviceDictionary(void) {
    if (!deviceDictionary) {
        deviceDictionary = @{
            @"iPhone10,1": @"iPhone 8", //国行(A1863)、日行(A1906)
            @"iPhone10,2": @"iPhone 8 Plus", //国行(A1864)、日行(A1898)
            @"iPhone10,3": @"iPhone X", //国行(A1865)、日行(A1902)
            @"iPhone10,4": @"iPhone 8", //美版(Global/A1905)
            @"iPhone10,5": @"iPhone 8 Plus", //美版(Global/A1897)
            @"iPhone10,6": @"iPhone X", //美版(Global/A1901)
            @"iPhone11,8": @"iPhone XR",
            @"iPhone11,2": @"iPhone XS",
            @"iPhone11,6": @"iPhone XS Max",
            @"iPhone11,4": @"iPhone XS Max",
            @"iPhone12,1": @"iPhone 11",
            @"iPhone12,3": @"iPhone 11 Pro",
            @"iPhone12,5": @"iPhone 11 Pro Max",
            @"iPhone12,8": @"iPhone SE (第2代)",
            @"iPhone13,1": @"iPhone 12 mini",
            @"iPhone13,2": @"iPhone 12",
            @"iPhone13,3": @"iPhone 12 Pro",
            @"iPhone13,4": @"iPhone 12 Pro Max",
            @"iPhone14,2": @"iPhone 13 Pro",
            @"iPhone14,3": @"iPhone 13 Pro Max",
            @"iPhone14,4": @"iPhone 13 mini",
            @"iPhone14,5": @"iPhone 13",
            @"iPhone14,6": @"iPhone SE (第2代)",
            @"iPhone14,7": @"iPhone 14",
            @"iPhone14,8": @"iPhone 14 Plus",
            @"iPhone15,2": @"iPhone 14 Pro",
            @"iPhone15,3": @"iPhone 14 Pro Max",
            @"iPhone15,4": @"iPhone 15",
            @"iPhone16,1": @"iPhone 15",
            //iPad
            @"iPad1,1": @"iPad",
            @"iPad1,2": @"iPad 3G",
            @"iPad2,1": @"iPad 2 (WiFi)",
            @"iPad2,2": @"iPad 2",
            @"iPad2,3": @"iPad 2 (CDMA)",
            @"iPad2,4": @"iPad 2",
            @"iPad2,5": @"iPad Mini (WiFi)",
            @"iPad2,6": @"iPad Mini",
            @"iPad2,7": @"iPad Mini (GSM+CDMA)",
            @"iPad3,1": @"iPad 3 (WiFi)",
            @"iPad3,2": @"iPad 3 (GSM+CDMA)",
            @"iPad3,3": @"iPad 3",
            @"iPad3,4": @"iPad 4 (WiFi)",
            @"iPad3,5": @"iPad 4",
            @"iPad3,6": @"iPad 4 (GSM+CDMA)",
            @"iPad4,1": @"iPad Air (WiFi)",
            @"iPad4,2": @"iPad Air (Cellular)",
            @"iPad4,3": @"iPad Air",
            @"iPad4,4": @"iPad Mini 2 (WiFi)",
            @"iPad4,5": @"iPad Mini 2 (Cellular)",
            @"iPad4,6": @"iPad Mini 2",
            @"iPad4,7": @"iPad Mini 3",
            @"iPad4,8": @"iPad Mini 3",
            @"iPad4,9": @"iPad Mini 3",
            @"iPad5,1": @"iPad Mini 4 (WiFi)",
            @"iPad5,2": @"iPad Mini 4 (LTE)",
            @"iPad5,3": @"iPad Air 2",
            @"iPad5,4": @"iPad Air 2",
            @"iPad6,3": @"iPad Pro 9.7",
            @"iPad6,4": @"iPad Pro 9.7",
            @"iPad6,7": @"iPad Pro 12.9",
            @"iPad6,8": @"iPad Pro 12.9",
            @"iPad6,11": @"iPad 5th",
            @"iPad6,12": @"iPad 5th",
            @"iPad7,1": @"iPad Pro 12.9 2nd",
            @"iPad7,2": @"iPad Pro 12.9 2nd",
            @"iPad7,3": @"iPad Pro 10.5",
            @"iPad7,4": @"iPad Pro 10.5",
            @"iPad7,5": @"iPad 6th",
            @"iPad7,6": @"iPad 6th",
            @"iPad8,1": @"iPad Pro 11",
            @"iPad8,2": @"iPad Pro 11",
            @"iPad8,3": @"iPad Pro 11",
            @"iPad8,4": @"iPad Pro 11",
            @"iPad8,5": @"iPad Pro 12.9 3rd",
            @"iPad8,6": @"iPad Pro 12.9 3rd",
            @"iPad8,7": @"iPad Pro 12.9 3rd",
            @"iPad8,8": @"iPad Pro 12.9 3rd",
            @"iPad11,1": @"iPad mini 5th",
            @"iPad11,2": @"iPad mini 5th",
            @"iPad11,3": @"iPad Air 3rd",
            @"iPad11,4": @"iPad Air 3rd",
            @"iPad11,6": @"iPad 8th",
            @"iPad11,7": @"iPad 8th",
            @"iPad12,1": @"iPad 9th",
            @"iPad12,2": @"iPad 9th",
            @"iPad14,1": @"iPad mini 6th",
            @"iPad14,2": @"iPad mini 6th",
            //iPod
            @"iPod1,1": @"iPod Touch 1G",
            @"iPod2,1": @"iPod Touch 2G",
            @"iPod3,1": @"iPod Touch 3G",
            @"iPod4,1": @"iPod Touch 4G",
            @"iPod5,1": @"iPod Touch (第5代)",
            @"iPod7,1": @"iPod Touch (第6代)",
            @"iPod9,1": @"iPod Touch (第7代)",
            //Simulator
            @"i386": @"Simulator",
            @"x86_64": @"Simulator"
        };
    }
    return deviceDictionary;
}


//获取设备型号名称
NSString *getDeviceModelName(void) {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if (!deviceString) return @"iPhone";
    return getDeviceDictionary()[deviceString];
}

